package cc.rengu.igas.momp.core.service.trans;

import cc.rengu.igas.momp.common.constant.CommonConstant;
import cc.rengu.igas.momp.common.dao.CouponInfoMapper;
import cc.rengu.igas.momp.common.dao.McDetailInfoMapper;
import cc.rengu.igas.momp.common.dao.MchntInfoMapper;
import cc.rengu.igas.momp.common.dao.PlatBizTmpMapper;
import cc.rengu.igas.momp.common.dao.impl.CouponInfoMapperImpl;
import cc.rengu.igas.momp.common.dao.impl.McDetailInfoMapperImpl;
import cc.rengu.igas.momp.common.dao.impl.MchntInfoMapperImpl;
import cc.rengu.igas.momp.common.dao.impl.PlatBizTmpMapperImpl;
import cc.rengu.igas.momp.common.entity.*;
import cc.rengu.igas.momp.common.enums.QueryRoleEnum;
import cc.rengu.igas.momp.common.enums.QueryTypeEnum;
import cc.rengu.igas.momp.common.enums.RespCodeEnum;
import cc.rengu.igas.momp.common.util.CommonMessageCheck;
import cc.rengu.igas.momp.facade.bean.CouponListInfoBean;
import cc.rengu.igas.momp.facade.request.QueryCouponDetailRequest;
import cc.rengu.igas.momp.facade.response.QueryCouponDetailResponse;
import cc.rengu.oltp.service.base.RadpService;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.enums.ChannelEnum;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.bean.PageInfo;
import cc.rengu.oltp.utility.bean.PageResult;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.base.StringUtil;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 卡券信息查询功能实现
 *
 * @author Jinan Liu
 * @date 2020/4/7 15:18
 */
public class QueryCouponInfoService extends RadpService {

    private String globalInstId;
    private String globalQueryType;
    private String globalQueryRole;
    private String globalQueryId;
    private String globalQueryOrgId;
    private String globalMcNo;
    private String pageIndex;
    private String pageSize;

    @Override
    protected int bizInit(String s) {
        return 0;
    }

    @Override
    protected int bizProcess(String s, int i) {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();

        try {

            QueryCouponDetailRequest queryCouponDetailRequest = new QueryCouponDetailRequest();
            ConvertUtil.convertOutput(queryCouponDetailRequest);

            QueryCouponDetailResponse queryCouponDetailResponse = new QueryCouponDetailResponse();
            queryCouponDetailResponse.setHeader(queryCouponDetailRequest.getHeader());
            BizResponse<QueryCouponDetailResponse> bizResponse = new BizResponse<>();
            bizResponse.setResult(queryCouponDetailResponse);

            String timeStamp = new SimpleDateFormat(CommonConstant.TIMESTAMP_FORMAT).format(new Date());
            globalQueryType = queryCouponDetailRequest.getQueryType();
            globalQueryId = queryCouponDetailRequest.getQueryId();
            globalQueryOrgId = queryCouponDetailRequest.getQueryOrgId();
            globalQueryRole = queryCouponDetailRequest.getQueryRole();
            globalInstId = queryCouponDetailRequest.getHeader().getInstId();
            globalMcNo = queryCouponDetailRequest.getMcNo();
            pageIndex = queryCouponDetailRequest.getPageIndex();
            pageSize = queryCouponDetailRequest.getPageSize();
            rglog.info("机构<{}>在<{}>由<{}>开始查询卡券类营销活动详细信息流程", globalInstId, timeStamp, globalQueryId);

            /* 报文检查 */
            CommonMessageCheck.checkBizMessageHeader(globalQueryType, globalQueryId, globalQueryRole, rglog);

            /* 检查操作员所属机构号 */
            if (StringUtil.isNullorEmpty(globalQueryOrgId)) {
                rglog.error("法人机构号<{}>的操作员<{}>的所属机构为空!", globalInstId, globalQueryId);
                throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
            }
            if (!StringUtil.isNullorEmpty(queryCouponDetailRequest.getMcNo())) {
                globalMcNo = queryCouponDetailRequest.getMcNo();
            }
            /* 检查营销活动编号 */
            if (StringUtil.isNullorEmpty(globalMcNo)) {
                rglog.error("法人机构号<{}>的操作员<{}>的营销活动编号为空!", globalInstId, globalQueryId);
                throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
            }


            // 服务调用
            BizResponse<QueryCouponDetailResponse> bizResponseNew = queryCouponDetailInfoList(xmlTreeUtil, queryCouponDetailRequest, queryCouponDetailResponse);

            ConvertUtil.convertInput(bizResponseNew.getResult());

        } catch (BizException e) {
            try {
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_CODE, e.getCode());
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_DESC, e.getMessage());
            } catch (Exception e1) {
                rglog.error(StringUtil.ExceptionToString(e1));
            }
        } catch (Exception e) {
            rglog.error(StringUtil.ExceptionToString(e));
            try {
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_CODE, OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespCode());
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_DESC, OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespDesc());
            } catch (Exception e1) {
                rglog.error(StringUtil.ExceptionToString(e1));
            }
        }

        return CommonConstant.PROCESS_SUCCESS;
    }

    @Override
    protected int bizTerminate(String s) {
        return 0;
    }


    /**
     * 查询卡券详细信息
     * 银行人员可以查询下辖指定的所有出资类型的卡券类营销活动的卡券信息
     * 商户人员可以查询商户自己创建的指定的卡券类营销活动的卡券信息
     * 用户可以查询自己已领取的指定营销活动的卡券信息
     *
     * @param xmlTreeUtil               内部XML树
     * @param queryCouponDetailRequest  卡券类活动详情查询接口请求报文
     * @param queryCouponDetailResponse 卡券类活动详情查询接口应答报文
     * @return cc.rengu.oltp.service.model.BizResponse<cc.rengu.igas.momp.facade.response.QueryCouponMcDetailInfoResponse> 卡券类活动详情查询接口应答报文
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @since 2020/5/28 14:50
     */
    private BizResponse<QueryCouponDetailResponse> queryCouponDetailInfoList(XmlTreeUtil xmlTreeUtil, QueryCouponDetailRequest queryCouponDetailRequest, QueryCouponDetailResponse queryCouponDetailResponse) throws Exception {

        BizResponse<QueryCouponDetailResponse> bizResponse = new BizResponse<>();

        /* 判断查询类型 */
        CpMcListInfoInObj cpMcListInfoInObj = new CpMcListInfoInObj();

        /* 机构编码 */
        cpMcListInfoInObj.setInstId(globalInstId);

        /* 营销活动编号 */
        cpMcListInfoInObj.setMcNo(globalMcNo);
        /* 领取人 */
        cpMcListInfoInObj.setClaimUserId(queryCouponDetailRequest.getClaimUser());
        /* 卡券Id*/
        cpMcListInfoInObj.setCouponNo(queryCouponDetailRequest.getCouponNo());


        /* 先查询所有数据，在判断查询用户是否有权限获取到数据 */
        /* 根据角色权限查询数据 */
        switch (QueryTypeEnum.getByValue(globalQueryType)) {
            case BANK_QUERY_TYPE:
                /* 银行人员,需要拼接营销活动机构信息表查询 */

                /* 机构编号 */
                if (!StringUtil.isNullorEmpty(globalQueryOrgId)) {
                    cpMcListInfoInObj.setOrgId(globalQueryOrgId);
                }

                /* 银行人员查询营销活动详细信息 */
                queryCouponMcDetailInfoByBank(queryCouponDetailResponse, cpMcListInfoInObj);

                break;

            case MCHNT_QYERY_TYPE:
                /* 商户人员,需要拼接营销活动商户信息表查询 */

                /* 根据商户编号查询商圈编号 */
                String mchntNo = globalQueryId;
                String mchntArea = null;
                TBMchntBaseInfo tbMchntBaseInfo = null;
                if (!StringUtil.isNullorEmpty(mchntNo)) {
                    /* 查询商户基本信息表 */
                    tbMchntBaseInfo = selectMompMchntBaseInfoByPrimaryKey(globalInstId, mchntNo);
                    if (null == tbMchntBaseInfo) {
                        rglog.error("查询法人机构<{}>商户<{}>基本信息失败!", globalInstId, mchntNo);
                        throw new BizException(RespCodeEnum.NO_TRANS_RECORD.getRespCode(), RespCodeEnum.NO_TRANS_RECORD.getRespDesc());
                    } else {
                        /* 商户编号 */
                        if (!StringUtil.isNullorEmpty(mchntNo)) {
                            cpMcListInfoInObj.setMchntNo(mchntNo);
                        }

                        /* 商圈编号 */
                        mchntArea = tbMchntBaseInfo.getMchntArea();
                        if (!StringUtil.isNullorEmpty(mchntArea)) {
                            cpMcListInfoInObj.setMchntArea(mchntArea);
                        }
                    }
                }

                /* 商户查询营销活动详细信息 */
                queryCouponMcDetailInfoByMchnt(queryCouponDetailResponse, cpMcListInfoInObj);

                break;

            case USER_QYERY_TYPE:
                /* 用户直接查询卡券信息表 */
                queryCouponMcDetailInfoByUser(queryCouponDetailResponse, cpMcListInfoInObj);
                break;

            default:
                /* 其他查询角色不支持 */
                rglog.error("当前<{}>角色的查询用户<{}>无权限操作数据!", globalQueryRole, globalQueryType);
                throw new BizException(RespCodeEnum.NO_AUTHORIZATION_TO_OPER_ERROR.getRespCode(), RespCodeEnum.NO_AUTHORIZATION_TO_OPER_ERROR.getRespDesc());
        }

        bizResponse.setRspSysId(ChannelEnum.MOMP.getChannelType());
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());

        queryCouponDetailResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        queryCouponDetailResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(queryCouponDetailResponse);

        return bizResponse;
    }

    /**
     * 银行人员查询营销活动详细信息
     *
     * @param queryCouponDetailResponse 卡券信息查询接口应答对象
     * @param cpMcListInfoInObj         营销活动列表查询使用实体类入参
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/5/28 18:58     *
     */
    private void queryCouponMcDetailInfoByBank(QueryCouponDetailResponse queryCouponDetailResponse, CpMcListInfoInObj cpMcListInfoInObj) throws Exception {

        PageInfo pageInfo = new PageInfo();
        pageInfo.setPageIndex(Integer.parseInt(pageIndex));
        pageInfo.setPageSize(Integer.parseInt(pageSize));
        PageResult pageResult;

        if (QueryRoleEnum.BANK_BASIC_QUERY_ROLE.getQueryRoleCode().equals(globalQueryRole) ||
                QueryRoleEnum.BANK_PUBLISH_QYERY_ROLE.getQueryRoleCode().equals(globalQueryRole)) {

            TMMcCouponInfo tmMcCouponInfo = selectCouponMcDetailInfoByBankBasicRole(cpMcListInfoInObj);
            if (null == tmMcCouponInfo) {
                rglog.error("机构<{}>的卡券类营销活动<{}>的基本信息查询失败!用户无权限查看数据或不存在该营销活动", globalInstId, globalMcNo);
                throw new BizException(RespCodeEnum.GET_COUPON_MC_DETAIL_ERROR.getRespCode(), RespCodeEnum.GET_COUPON_MC_DETAIL_ERROR.getRespDesc());
            }

            /* 银行操作员和发布员都是从正式表查询数据,除了营销活动基本表外其他表查询语句相同 */

            /* 查询指定营销活动所有卡券明细 */
            pageResult = getAllCouponDetailInfo(cpMcListInfoInObj, pageInfo);

            /* 拼接交易信息 */
            List<TMCouponInfo> couponInfoList = new ArrayList<>();
            if (null == pageResult || null == pageResult.getPageInfo() || null == pageResult.getResult() || pageResult.getResult().isEmpty()) {
                rglog.info("根据查询条件查询到0条卡券信息!");

                queryCouponDetailResponse.setTotalPage(CommonConstant.ZERO_COMMON_CONSTANT);
                queryCouponDetailResponse.setTotalRows(CommonConstant.ZERO_COMMON_CONSTANT);

            } else {

                for (Object object : pageResult.getResult()) {
                    couponInfoList.add((TMCouponInfo) object);
                }

                rglog.info("SIZE=<{}>", pageResult.getResult().size());

                List<CouponListInfoBean> couponListInfoBeanList = couponInfoList.stream().map(this::convertCouponInfo).collect(Collectors.toList());
                queryCouponDetailResponse.setCouponInfoList(couponListInfoBeanList);

                queryCouponDetailResponse.setTotalPage(Integer.toString(pageResult.getPageInfo().getTotalPage()));
                queryCouponDetailResponse.setTotalRows(Integer.toString(pageResult.getPageInfo().getTotalRows()));

            }

        } else {
            /* 其他查询角色不支持 */
            rglog.error("当前<{}>角色的查询用户<{}>无权限操作数据!", globalQueryRole, globalQueryType);
            throw new BizException(RespCodeEnum.NO_AUTHORIZATION_TO_OPER_ERROR.getRespCode(), RespCodeEnum.NO_AUTHORIZATION_TO_OPER_ERROR.getRespDesc());
        }
    }

    /**
     * 银行操作员查询营销活动基本信息表数据(正式表)
     *
     * @param cpMcListInfoInObj 营销活动列表查询使用实体类入参
     * @return cc.rengu.igas.momp.common.entity.TMMcCouponInfo 卡券类营销活动信息表
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/5/28 19:11
     */
    private TMMcCouponInfo selectCouponMcDetailInfoByBankBasicRole(CpMcListInfoInObj cpMcListInfoInObj) throws Exception {
        McDetailInfoMapper mcDetailInfoMapper = new McDetailInfoMapperImpl();
        return mcDetailInfoMapper.selectCouponMcDetailInfoByBankBasicRole(cpMcListInfoInObj);
    }

    /**
     * 银行审批员查询营销活动基本信息表数据
     *
     * @param cpMcListInfoInObj 营销活动列表查询使用实体类入参
     * @return cc.rengu.igas.momp.common.entity.TMPlatBizTmp 业务数据临时表
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/5/28 19:11
     */
    private TMPlatBizTmp selectCouponMcDetailInfoByBankAuditRole(CpMcListInfoInObj cpMcListInfoInObj) throws Exception {
        PlatBizTmpMapper platBizTmpMapper = new PlatBizTmpMapperImpl();
        return platBizTmpMapper.selectCouponMcDetailInfoByBankAuditRole(cpMcListInfoInObj);
    }

    /**
     * 银行发布员查询营销活动基本信息表数据
     *
     * @param cpMcListInfoInObj 营销活动列表查询使用实体类入参
     * @return cc.rengu.igas.momp.common.entity.TMMcCouponInfo 卡券类营销活动信息表
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/5/28 19:11
     */
    private TMMcCouponInfo selectCouponMcDetailInfoByBankPublishRole(CpMcListInfoInObj cpMcListInfoInObj) throws Exception {
        McDetailInfoMapper mcDetailInfoMapper = new McDetailInfoMapperImpl();
        return mcDetailInfoMapper.selectCouponMcDetailInfoByBankPublishRole(cpMcListInfoInObj);
    }

    /**
     * 商户查询营销活动卡券信息
     *
     * @param queryCouponDetailResponse 卡券信息查询接口应答对象
     * @param cpMcListInfoInObj         营销活动列表查询使用实体类入参
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/5/28 19:23
     */
    private void queryCouponMcDetailInfoByMchnt(QueryCouponDetailResponse queryCouponDetailResponse, CpMcListInfoInObj cpMcListInfoInObj) throws Exception {

        PageInfo pageInfo = new PageInfo();
        pageInfo.setPageIndex(Integer.parseInt(pageIndex));
        pageInfo.setPageSize(Integer.parseInt(pageSize));
        PageResult pageResult;

        /* 查询营销活动基本信息 */
        TMMcCouponInfo tmMcCouponInfo;
        if (StringUtil.isNullorEmpty(cpMcListInfoInObj.getMchntArea())) {
            tmMcCouponInfo = selectCouponMcDetailInfoByMchnt(cpMcListInfoInObj);
        } else {
            tmMcCouponInfo = selectCouponMcDetailInfoByMchntWithArea(cpMcListInfoInObj);
        }
        if (null == tmMcCouponInfo) {
            rglog.error("机构<{}>的卡券类营销活动<{}>的基本信息查询失败!商户无权限查看数据或不存在该营销活动", globalInstId, globalMcNo);
            throw new BizException(RespCodeEnum.GET_MONEY_OFF_MC_DETAIL_ERROR.getRespCode(), RespCodeEnum.GET_MONEY_OFF_MC_DETAIL_ERROR.getRespDesc());
        }

        /* 商户仅可查询自己创建的卡券类营销活动的卡券信息 */
//        if(!CommonConstant.SPONSOR_MCHNT.equals(tmMcCouponInfo.getSponsor())) {
//            rglog.error("商户仅可查询自己创建的卡券类营销活动的卡券信息! INST_ID=<{}>, MC_NO=<{}>, SPONSOR=<{}>", globalInstId, globalMcNo, tmMcCouponInfo.getSponsor());
//            throw new BizException(RespCodeEnum.MCHNT_ONLY_QUERY_CP_ONESELF.getRespCode(), RespCodeEnum.MCHNT_ONLY_QUERY_CP_ONESELF.getRespDesc());
//        }

        /* 查询指定营销活动所有卡券明细 */
        pageResult = getAllCouponDetailInfo(cpMcListInfoInObj, pageInfo);

        /* 拼接交易信息 */
        List<TMCouponInfo> couponInfoList = new ArrayList<>();

        //此处校验 解决 dbsutil 中查询数据为空时返回null 的问题
        if (null == pageResult || null == pageResult.getPageInfo() || null == pageResult.getResult() || pageResult.getResult().isEmpty()) {
            rglog.info("根据查询条件查询到0条卡券信息!");

            queryCouponDetailResponse.setTotalPage(CommonConstant.ZERO_COMMON_CONSTANT);
            queryCouponDetailResponse.setTotalRows(CommonConstant.ZERO_COMMON_CONSTANT);

        } else {

            for (Object object : pageResult.getResult()) {
                couponInfoList.add((TMCouponInfo) object);
            }

            rglog.info("SIZE=<{}>", pageResult.getResult().size());

            List<CouponListInfoBean> couponListInfoBeanList = couponInfoList.stream().map(this::convertCouponInfo).collect(Collectors.toList());
            queryCouponDetailResponse.setCouponInfoList(couponListInfoBeanList);

            queryCouponDetailResponse.setTotalPage(Integer.toString(pageResult.getPageInfo().getTotalPage()));
            queryCouponDetailResponse.setTotalRows(Integer.toString(pageResult.getPageInfo().getTotalRows()));

        }
    }

    /**
     * 商户查询营销活动基本信息表数据
     *
     * @param cpMcListInfoInObj 营销活动列表查询使用实体类入参
     * @return cc.rengu.igas.momp.common.entity.TMMcCouponInfo 卡券类营销活动信息表
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/5/28 19:10
     */
    private TMMcCouponInfo selectCouponMcDetailInfoByMchnt(CpMcListInfoInObj cpMcListInfoInObj) throws Exception {
        McDetailInfoMapper mcDetailInfoMapper = new McDetailInfoMapperImpl();
        return mcDetailInfoMapper.selectCouponMcDetailInfoByMchnt(cpMcListInfoInObj);
    }

    /**
     * 商户查询营销活动基本信息表数据
     *
     * @param cpMcListInfoInObj 营销活动列表查询使用实体类入参
     * @return cc.rengu.igas.momp.common.entity.TMMcCouponInfo 卡券类营销活动信息表
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/5/28 19:10
     */
    private TMMcCouponInfo selectCouponMcDetailInfoByMchntWithArea(CpMcListInfoInObj cpMcListInfoInObj) throws Exception {
        McDetailInfoMapper mcDetailInfoMapper = new McDetailInfoMapperImpl();
        return mcDetailInfoMapper.selectCouponMcDetailInfoByMchntWithArea(cpMcListInfoInObj);
    }

    /**
     * 用户查询已领取卡券信息
     *
     * @param queryCouponDetailResponse 卡券信息查询接口应答对象
     * @param cpMcListInfoInObj         营销活动列表查询使用实体类入参
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/5/28 19:23
     */
    private void queryCouponMcDetailInfoByUser(QueryCouponDetailResponse queryCouponDetailResponse, CpMcListInfoInObj cpMcListInfoInObj) throws Exception {

        /* 查询指定营销活动所有卡券明细 */
        if (CommonConstant.MINUS_ONE_COMMON_CONSTANT.equals(pageIndex)) {
            /* 不分页查询流程 */
            List<TMCouponInfo> couponInfoList = getAllUserCouponDetailInfo(globalInstId, globalMcNo, globalQueryId);
            if (null == couponInfoList || couponInfoList.isEmpty()) {
                /* 未查询到数据 */
                queryCouponDetailResponse.setCouponInfoList(null);
                queryCouponDetailResponse.setTotalPage(CommonConstant.ZERO_COMMON_CONSTANT);
                queryCouponDetailResponse.setTotalRows(CommonConstant.ZERO_COMMON_CONSTANT);
            } else {
                /* 查询到数据 */
                List<CouponListInfoBean> couponListInfoBeanList = couponInfoList.stream().map(this::convertCouponInfo).collect(Collectors.toList());
                queryCouponDetailResponse.setCouponInfoList(couponListInfoBeanList);
                queryCouponDetailResponse.setTotalPage(CommonConstant.ONE_COMMON_CONSTANT);
                queryCouponDetailResponse.setTotalRows(Integer.toString(couponListInfoBeanList.size()));
            }

        } else {

            /* 分页查询流程 */
            cpMcListInfoInObj.setClaimUserId(globalQueryId);

            PageInfo pageInfo = new PageInfo();
            pageInfo.setPageIndex(Integer.parseInt(pageIndex));
            pageInfo.setPageSize(Integer.parseInt(pageSize));
            PageResult pageResult;

            pageResult = getAllCouponDetailInfoByUser(cpMcListInfoInObj, pageInfo);

            /* 拼接交易信息 */
            List<TMCouponInfo> couponInfoList = new ArrayList<>();
            if (null == pageResult || null == pageResult.getPageInfo() || null == pageResult.getResult() || pageResult.getResult().isEmpty()) {
                rglog.info("根据查询条件查询到0条卡券信息!");

                queryCouponDetailResponse.setTotalPage(CommonConstant.ZERO_COMMON_CONSTANT);
                queryCouponDetailResponse.setTotalRows(CommonConstant.ZERO_COMMON_CONSTANT);

            } else {

                for (Object object : pageResult.getResult()) {
                    couponInfoList.add((TMCouponInfo) object);
                }

                rglog.info("SIZE=<{}>", pageResult.getResult().size());

                List<CouponListInfoBean> couponListInfoBeanList = couponInfoList.stream().map(this::convertCouponInfo).collect(Collectors.toList());
                queryCouponDetailResponse.setCouponInfoList(couponListInfoBeanList);

                queryCouponDetailResponse.setTotalPage(Integer.toString(pageResult.getPageInfo().getTotalPage()));
                queryCouponDetailResponse.setTotalRows(Integer.toString(pageResult.getPageInfo().getTotalRows()));

            }
        }
    }

    /**
     * 银行人员查询卡券列表信息
     *
     * @param cpMcListInfoInObj 卡券类营销活动列表查询使用实体类入参
     * @param pageInfo          数据库操作分页实体类
     * @return cc.rengu.oltp.utility.bean.PageResult 查询结果列表
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/6/1 22:46
     */
    private PageResult getAllCouponDetailInfo(CpMcListInfoInObj cpMcListInfoInObj, PageInfo pageInfo) throws Exception {

        CouponInfoMapper couponInfoMapper = new CouponInfoMapperImpl();
        return couponInfoMapper.selectAllCouponInfoByMcNo(cpMcListInfoInObj, pageInfo);
    }

    /**
     * 银行人员查询卡券列表信息
     *
     * @param cpMcListInfoInObj 卡券类营销活动列表查询使用实体类入参
     * @param pageInfo          数据库操作分页实体类
     * @return cc.rengu.oltp.utility.bean.PageResult 查询结果列表
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/6/1 22:46
     */
    private PageResult getAllCouponDetailInfoByUser(CpMcListInfoInObj cpMcListInfoInObj, PageInfo pageInfo) throws Exception {

        CouponInfoMapper couponInfoMapper = new CouponInfoMapperImpl();
        return couponInfoMapper.getAllCouponDetailInfoByUser(cpMcListInfoInObj, pageInfo);
    }

    /**
     * 用户查询已领取的指定营销活动的卡券列表信息
     *
     * @param instId 法人机构号
     * @param mcNo   营销活动编号
     * @param userId 用户ID
     * @return java.util.List<cc.rengu.igas.momp.common.entity.TMCouponInfo> 卡券信息表
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/4/7 15:47
     */
    private List<TMCouponInfo> getAllUserCouponDetailInfo(String instId, String mcNo, String userId) throws Exception {

        CouponInfoMapper couponInfoMapper = new CouponInfoMapperImpl();
        return couponInfoMapper.selectAllCouponInfoByMcNo(instId, mcNo, userId);
    }

    /**
     * 数据库实体类和接口实体类数据转换
     *
     * @param couponInfo
     * @return cc.rengu.igas.momp.facade.bean.CouponListInfoBean
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/4/7 15:04
     */
    private CouponListInfoBean convertCouponInfo(TMCouponInfo couponInfo) {
        CouponListInfoBean couponListInfoBean = new CouponListInfoBean();
        try {

            /* 卡券编号 */
            couponListInfoBean.setCouponNo(couponInfo.getCouponNo());
            /* 卡券有效起始日期 */
            couponListInfoBean.setCouponStartDate(couponInfo.getCouponStartDate());
            /* 卡券有效结束日期 */
            couponListInfoBean.setCouponEndDate(couponInfo.getCouponEndDate());
            /* 领取日期 */
            couponListInfoBean.setClaimDate(couponInfo.getClaimDate());
            /* 领取时间 */
            couponListInfoBean.setClaimTime(couponInfo.getClaimTime());
            /* 卡券状态 */
            couponListInfoBean.setCouponStatus(couponInfo.getCouponStatus());
            /* 卡券可抵扣金额 */
            couponListInfoBean.setCouponAmt(couponInfo.getCouponAmt());
            /* 领取人 */
            couponListInfoBean.setClaimUser(couponInfo.getClaimUserId());
            /* 领取方式 */
            couponListInfoBean.setClaimType(couponInfo.getClaimType());

        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("转换营销活动信息异常,异常信息:<{}>", byteArrayOutputStream.toString());
            throw new BizException(RespCodeEnum.SYSTEM_ERROR.getRespCode(), RespCodeEnum.SYSTEM_ERROR.getRespDesc());
        }
        return couponListInfoBean;
    }

    /**
     * 使用商户编号从内管商户基本信息表查询商户所属商圈编号
     *
     * @param globalInstId 法人机构号
     * @param mchntNo      商户编号
     * @return cc.rengu.igas.momp.common.entity.TBMchntBaseInfo 商户基本信息表T_B_MCHNT_BASE_INFO
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/5/28 13:06
     */
    private TBMchntBaseInfo selectMompMchntBaseInfoByPrimaryKey(String globalInstId, String mchntNo) throws Exception {

        MchntInfoMapper mchntInfoMapper = new MchntInfoMapperImpl();
        return mchntInfoMapper.selectMompMchntBaseInfoByPrimaryKey(globalInstId, mchntNo);
    }
}
